const http = require('http');
const fs = require('fs');
const urlLib = require('url');
const queryString = require('querystring');

const server = http.createServer((req, res) => {
    /**
     * GET 
     */

    let obj = urlLib.parse(req.url, true);
    const GET = obj.query;
    const url = obj.pathname;


    /**
     * POST
     */
    let str = '';
    req.on('data', (data) => {
        str += data;
    });
    req.on('end', () => {
        const POST = queryString.parse(str);
        console.log(url, GET, POST);
    });
    
    /**
     * 访问文件
     */
    fs.readFile(`./www${url}`, (err, data) => {
        if (err) {
            res.write('404找不到页面');
        } else {
            res.write(data);
        }
        res.end();
    });
});

server.listen(8080, () => {
    console.log('访问成功');
})